package com.hmall.cart.listener;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmall.cart.domain.po.Cart;
import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.domain.CartClearMessageDTO;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @Author 86157
 * @Date 2025/9/4 下午9:07
 */
@Component
public class CartClearListener {

    @Autowired
    private ICartService cartService;
        @RabbitListener(bindings = @QueueBinding(
                value = @Queue(value = MqConstants.CART_SUCCESS_QUEUE, durable = "true"),
                exchange = @Exchange(value = MqConstants.CART_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
                key = MqConstants.CART_SUCCESS_KEY
        ))
        public void listenCartClear(CartClearMessageDTO dto) {
            cartService.remove(Wrappers.<Cart>lambdaQuery()
                    .eq(Cart::getUserId, dto.getUserId())
                    .in(Cart::getItemId, dto.getItemIds()));
        }

}
